package com.bangyo.usertype;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.HibernateException;

import com.bangyo.file.Purpose;

public class PurposeUserType extends AbstractTypeNameUserType
{
    public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
        throws HibernateException, SQLException
    {
        String purpose = rs.getString(names[0]);
        return rs.wasNull()?null:Purpose.toPurpose(purpose);
    }

    public void nullSafeSet(PreparedStatement st, Object value, int index)
        throws HibernateException, SQLException
    {
        if (value==null)
        {
            throw new HibernateException("purpose cannot be null");
        }
        else
        {
            st.setString(index, ((Purpose)value).getPurpose());
        }
    }

    public Class<?> returnedClass()
    {
        return Purpose.class;
    }
}
